package com.woniu.dao;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.woniu.entity.Auctiongoods;
import com.woniu.entity.Goods;
import com.woniu.entity.Order;
import com.woniu.entity.Record;

import tk.mybatis.mapper.common.Mapper;

public interface OrderMapper extends Mapper<Order> {

	/**
	 * @return 累计成交金额
	 */
	Double getTotalDealAmount();

	/**
	 * @return 今日成交金额
	 */
	Double getDealAmountToday();

	/**
	 * @return 累计成交商品数
	 */
	Integer getTotalDealProductCount();

	/**
	 * @return 今日成交商品数
	 */
	Integer getTodayDealProductCount();

	/**
	 * @return 累计成交用户数
	 */

	Integer getDealUserCount();

	/**
	 * @return 昨日流水
	 */
	@Select("select sum(ordertotalprice) from t_order WHERE (TO_DAYS(NOW()))-TO_DAYS(dealtime)=1")
	Double getTradingFlowYesterday();

	/**
	 * @return 本周流水
	 */
	@Select("select sum(ordertotalprice) from t_order WHERE YEARWEEK(date_format(dealtime,'%Y-%m-%d')) = YEARWEEK(now())")
	Double getTradingFlowThisWeek();

	/**
	 * @return
	 */
	@Select("select sum(ordertotalprice) from t_order WHERE TO_DAYS(dealtime)=to_days(now())")
	Double getTradingFlowToday();

	/**
	 * @return
	 */
	@Select("select sum(ordertotalprice) from t_order WHERE DATE_FORMAT( dealtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
	Double getTradingFlowThisMonth();

	/**
	 * @return
	 */
	@Select("select sum(commision) from t_order WHERE TO_DAYS(dealtime)=to_days(now())")
	Double commissionRevenueToday();

	/**
	 * @return
	 */
	@Select("select sum(commision) from t_order WHERE (TO_DAYS(NOW()))-TO_DAYS(dealtime)=1")
	Double commissionRevenueYesterday();

	/**
	 * @return
	 */
	@Select("select sum(commision) from t_order WHERE YEARWEEK(date_format(dealtime,'%Y-%m-%d')) = YEARWEEK(now())")
	Double commissionRevenueThisWeek();

	/**
	 * @return
	 */
	@Select("select sum(commision) from t_order WHERE DATE_FORMAT( dealtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
	Double commissionRevenueThisMonth();

	/**
	 * @return 当日成交金额
	 */
	@Select("select sum(dealprice) from t_order WHERE TO_DAYS(dealtime)=to_days(now())")
	Double dealAmountToday();

	/**
	 * @return 昨日成交金额
	 */
	@Select("select sum(dealprice) from t_order WHERE (TO_DAYS(NOW()))-TO_DAYS(dealtime)=1")
	Double dealAmountYesterday();

	/**
	 * @return 本周成交金额
	 */
	@Select("select sum(dealprice) from t_order WHERE YEARWEEK(date_format(dealtime,'%Y-%m-%d')) = YEARWEEK(now())")
	Double dealAmountThisWeek();

	/**
	 * @return 本月成交金额
	 */
	@Select("select sum(dealprice) from t_order WHERE DATE_FORMAT( dealtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
	Double dealAmountThisMonth();

	/**
	 * @return 当日订单数
	 */
	@Select("select count(1) from t_order WHERE TO_DAYS(dealtime)=to_days(now())")
	Integer dealOrderCountToday();

	/**
	 * @return 昨日订单数
	 */
	@Select("select count(1) from t_order WHERE (TO_DAYS(NOW()))-TO_DAYS(dealtime)=1")
	Integer dealOrderCountYesterday();

	/**
	 * @return 本周订单数
	 */
	@Select("select count(1) from t_order WHERE YEARWEEK(date_format(dealtime,'%Y-%m-%d')) = YEARWEEK(now())")
	Integer dealOrderCountThisWeek();

	/**
	 * @return 本月订单数
	 */
	@Select("select count(1) from t_order WHERE DATE_FORMAT( dealtime, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )")
	Integer dealOrderCountThisMonth();

	/**
	 * @return
	 */

	List<Map> getDealAmountByDate();

	List<Map> getOrderCountByDate();

	// 王世嘉
	@Select("select uname from t_user where uid=#{value}")
	String selectNameById(Integer uid);

	@Select("select uid from t_user where uname=#{value}")
	Integer selectByUname(String uname);

	List<Order> selectOrderByBidAndSid(@Param("bid") Integer bid, @Param("sid") Integer sid,
			@Param("firstTime") Date firstTime, @Param("lastTime") Date lastTime, @Param("timeChoose") int timeChoose,
			@Param("moneyChoose") int moneyChoose);

	@Delete("delete from t_order where oid=#{value}")
	int deleteOrderByOid(Integer oid);

	@Select("select gname from t_goods where gid=#{value}")
	String selectGnameByGid(Integer gid);

	@Select("select deposit from t_auctiongoods where agid=(select gid from t_auctiongoods where gid=#{value})")
	double selectDepositByGid(Integer gid);

	
	@Select("SELECT * FROM t_auctiongoods WHERE endingtime IS NOT NULL AND timestampdiff(second,endingtime,now())<=360 and timestampdiff(second,endingtime,now())>=0")
	List<Auctiongoods> selectAuctiongoods();

	@Select("select * from t_goods where gid=#{value}")
	List<Goods> selectGoods(Integer gid);

	@Select("select * from t_record where recordprice=(select max(recordprice) FROM t_record where gid=#{value}) and gid=#{value}")
	Record selectRecord(Integer gid);

	@Select("select getid from t_getaddress where uid=#{value}")
	Integer selectGetidByUsid(Integer uSid);

	@Select("select deposit from t_auctiongoods where agid=#{value}")
	double selectDepositByAgid(Integer agid);

//	@Update("update t_auctiongoods set auctionstatus='竞价成功' where gid=#{value}")
//	void updateGoodstatus(Integer gid);

}